<?php

/**
 * Loobica user session library
 * 
 * @project : Loobica Ads
 * @author  : Dino (DBK)
 * @date    : 13 Jan 2014
 * 
 */
if (!defined('BASEPATH'))
  exit('No direct script access allowed');

class User_session {

  private $ci;

  public function __construct() {
    $this->ci = & get_instance();
  }

  private function set_sign_id($user_id = 0) {
    return $this->ci->session->set_userdata('loobica_user', (int) $user_id);
  }

  private function get_sign_id() {
    return (int) $this->ci->session->userdata('loobica_user');
  }

  public function sign_in($email = null, $password = null) {
    $user_data = $this->ci->db
                    ->select('user_id')
                    ->where(array('user_email' => (string) $email, 'user_password' => (string) $this->ci->loobica_hash($password), 'user_status' => 1))
                    ->get('user')->row_array();
    if (!empty($user_data['user_id'])) {
      $this->set_sign_id($user_data['user_id']);
      return true;
    } else {
      return false;
    }
  }

  public function sign_on($user_id = 0) {
    $user_data = $this->ci->db
                    ->select('user_id')
                    ->where(array('user_id' => (int) $user_id, 'user_status' => 1))
                    ->get('user')->row_array();
    if (!empty($user_data['user_id'])) {
      $this->set_sign_id($user_data['user_id']);
      return true;
    } else {
      return false;
    }
  }

  public function sign_out() {
    $this->ci->session->set_userdata('loobica_user', null);
    $this->ci->session->unset_userdata('loobica_user');
    return;
  }

  public function signed_user() {
    $user_data = array();
    $user_id = $this->get_sign_id();
    if (!empty($user_id)) {
      $user_data = $this->ci->db
                      ->select('*')
                      ->where(array('user_id' => $this->get_sign_id(), 'user_status' => 1))
                      ->get('user')->row_array();
    }
    return $user_data;
  }

}

?>